<?php
/**
 *
 */
class shop extends CI_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model('M_shop');
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('session');
    if($this->session->userdata('username')==null){
      redirect('/login');
    }
  }
  /**
  *店铺列表页
  */
  public function shop_list(){
    $this->load->library('pagination');
    $page = 0;
    $offset = 0;
    $per_page = 10;
    if(!empty($_GET['per_page'])){
      $page = $_GET['per_page'];
      $offset = ($page-1)*$per_page;
    }
    $result = $this->M_shop->getShopList($per_page,$offset);
    if(!empty($_GET['search'])){
      $result = $this->M_shop->selectShop($_GET['search'],$per_page,$offset);
    }
    $config['base_url']=site_url('shop/shop_list');
    $config['total_rows'] = $result['total'];
    $config['per_page'] = $per_page;
    $this->pagination->initialize($config);
    $data['shop_list'] = $result['shoplist'];
    $data['pager'] = $this->pagination->create_links();
    $this->load->view('shop_list',$data);
  }
  /**
  *添加店铺页面
  */
  public function shop_add(){
    if($this->session->userdata('utype')==2){
      redirect('/home');
      return false;
    }
    $this->load->view('shop_add');
  }
  /**
  *店铺详情页
  */
  public function shop_detail(){
    if(empty($_GET['shopid'])){

    }else{
      $shopid = $_GET['shopid'];
      $this->load->library('pagination');
      $page = 0;
      $offset = 0;
      $per_page = 5;
      if(!empty($_GET['per_page'])){
        $page = $_GET['per_page'];
        $offset = ($page-1)*$per_page;
      }
      $result = $this->M_shop->getShopDetail($shopid,$per_page,$offset);
      if($result['flag']==0){
        redirect('/shop/shop_list');
      }
      $config['base_url']=site_url('shop/shop_detail');
      $config['total_rows'] = $result['total'];
      $config['per_page'] = $per_page;
      $this->pagination->initialize($config);
      $data['shop'] = $result['shopdetail'];
      $data['callback'] = $result['callback'];
      $data['shopid'] = $shopid;
      $data['pager'] = $this->pagination->create_links();
      $this->load->view('shop_detail',$data);
      // $result = $this->M_shop->getShopDetail($shopid);
    }
  }
  /**
  *添加店铺接口
  */
  public function shop_add_go(){
    $vali_rules = array(
      array(
        'field' => 'shopname',
        'label' => 'Shopname',
        'rules' => 'required'
      ),
      array(
        'field' => 'shopboss',
        'label' => 'Shopboss',
        'rules' => 'required'
      ),
      array(
        'field' => 'shoptel',
        'label' => 'Shoptel',
        'rules' => 'required'
      ),
      array(
        'field' => 'shopbrand',
        'label' => 'Shopbrand',
        'rules' => 'required'
      ),
      array(
        'field' => 'shopotherbrand',
        'label' => 'Shopotherbrand',
        'rules' => 'required'
      ),
      array(
        'field' => 'shopinfo',
        'label' => 'Shopinfo',
        'rules' => 'required'
      ),
      array(
        'field' => 'shopconsignee',
        'label' => 'Shopconsignee',
        'rules' => 'required'
      ),
      array(
        'field' => 'shopctel',
        'label' => 'Shopctel',
        'rules' => 'required'
      ),
      array(
        'field' => 'shopaddr',
        'label' => 'Shopaddr',
        'rules' => 'required'
      )
    );
    $this->form_validation->set_rules($vali_rules);
   if ($this->form_validation->run() == FALSE) {
     echo "false";
    }else {
      $shopinsert = array(
        'shopname'        => $_POST['shopname'],
        'shopboss'        => $_POST['shopboss'],
        'shoptel'         => $_POST['shoptel'],
        'shopbrand'       => $_POST['shopbrand'],
        'shopbrandname'   => $_POST['shopbrandname'],
        'shopotherbrand'  => $_POST['shopotherbrand'],
        'shopinfo'        => $_POST['shopinfo'],
        'shopconsignee'   => $_POST['shopconsignee'],
        'shopctel'        => $_POST['shopctel'],
        'shopaddr'        => $_POST['shopaddr']
      );
      echo $this->M_shop->addshop($shopinsert);
    }
  }
  /**
  *修改店铺接口
  */
  public function shop_update_go(){
    if(!empty($_POST['shopid']) && !empty($_POST['shopname']) && !empty($_POST['shopboss']) && !empty($_POST['shoptel']) && !empty($_POST['shopbrand']) && !empty($_POST['shopinfo']) && !empty($_POST['shopotherbrand']) && !empty($_POST['shopconsignee']) && !empty($_POST['shopctel']) && !empty($_POST['shopaddr'])){
      $option = array(
        'shopname'        => $_POST['shopname'],
        'shoptel'         => $_POST['shoptel'],
        'shopboss'        => $_POST['shopboss'],
        'shopbrand'       => $_POST['shopbrand'],
        'shopbrandname'   => $_POST['shopbrandname'],
        'shopinfo'        => $_POST['shopinfo'],
        'shopotherbrand'  => $_POST['shopotherbrand'],
        'shopconsignee'   => $_POST['shopconsignee'],
        'shopctel'        => $_POST['shopctel'],
        'shopaddr'        => $_POST['shopaddr'],
      );
      print_r($this->M_shop->updateShop($_POST['shopid'],$option));
    }
  }
  public function shopdelete(){
    if(!empty($_POST['shopid']) && $this->session->userdata('username')=='admin'){
      return $this->M_shop->deleteShop($_POST['shopid']);
    }else{
      echo 'false';
    }
  }
  /**
  *添加回访记录接口
  */
  public function shop_callback_go(){
    if(!empty($_GET['shopid']) && !empty($_GET['val'])){
      $flag = $this->M_shop->addShopCallback($_GET['shopid'],$_GET['val'],$this->session->userdata('username'));
      // echo $flag;
      // return false;
      if($flag>0){
        echo "true";
      }else {
        echo "false";
      }
    }else{
      echo "false";
    }
  }
  /**
  *修改回访记录接口
  */
  public function shop_callback_update(){
    $id = $_POST['id'];
    $val = $_POST['val'];
    $flag = $this->M_shop->updataCallback($id,$val);
    if($flag>0){
      echo "true";
    }else{
      echo "false";
    }
  }
  /**
  *删除回访记录接口
  */
  public function shop_callback_delete(){
    $id = $_POST['id'];
    $flag = $this->M_shop->deleteCallback($id);
    if($flag>0){
      echo "true";
    }else{
      echo "false";
    }
  }
  /**
  *查询个人回访记录
  */
  public function my_callback(){
    $this->load->library('pagination');
    $page = 0;
    $offset = 0;
    $per_page = 10;
    if(!empty($_GET['per_page'])){
      $page = $_GET['per_page'];
      $offset = ($page-1)*$per_page;
    }
    $result = $this->M_shop->getMycallback($this->session->userdata('username'),$per_page,$offset);
    if(!empty($_GET['search'])){
      $result = $this->M_shop->selectMycallback($_GET['search'],$this->session->userdata('username'),$per_page,$offset);
    }
    $config['base_url']=site_url('shop/my_callback');
    $config['total_rows'] = $result['total'];
    $config['per_page'] = $per_page;
    $this->pagination->initialize($config);
    $data['callback'] = $result['callback'];
    $data['pager'] = $this->pagination->create_links();
    $this->load->view('my_callback',$data);
  }
  /**
  *店铺列表导出EXCEL接口
  */
  public function shopexcel(){
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getActiveSheet()->setTitle('shops');
        $title = array(
          array('编号', '店名', '老板娘', '联系电话','合作品牌','其他公司品牌','店内信息','收货人','收货人电话','地址'),
          );
        $objPHPExcel->getActiveSheet() ->fromArray( $title, NULL, 'A1' );
        if(!empty($_GET['search'])){
          $shoplist = $this->M_shop->selectShop($_GET['search'],1,1);
        }else{
          $shoplist =  $this->M_shop->getShopList(1,1);
        }
        $result = $shoplist['totallist'];
        $i = 2;
        foreach ($result as $shop) {
          $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $shop['shopid']);
          $objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $shop['shopname']);
          $objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $shop['shopboss']);
          $objPHPExcel->getActiveSheet()->setCellValue('D'.$i, $shop['shoptel']);
          $objPHPExcel->getActiveSheet()->setCellValue('E'.$i, $shop['shopbrand']);
          $objPHPExcel->getActiveSheet()->setCellValue('F'.$i, $shop['shopotherbrand']);
          $objPHPExcel->getActiveSheet()->setCellValue('G'.$i, $shop['shopinfo']);
          $objPHPExcel->getActiveSheet()->setCellValue('H'.$i, $shop['shopconsignee']);
          $objPHPExcel->getActiveSheet()->setCellValue('I'.$i, $shop['shopctel']);
          $objPHPExcel->getActiveSheet()->setCellValue('J'.$i, $shop['shopaddr']);
          $i++;
        }
        // 保存excel—2007格式
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        //或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 非2007格式
        // 直接输出到浏览器
        // $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="店铺列表.xls"');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
      }
  /**
  *店铺业绩导出接口
  */
  public function shopbillexcel(){

  }
  /**
  *店铺业绩导出接口
  */
  public function getallshop(){
    $page = 1;
    $offset = 0;
    $per_page = 10;
    if(!empty($_GET['per_page'])){
      $page = $_GET['per_page'];
      $offset = ($page-1)*$per_page;
    }
    $result = $this->M_shop->getallshop($per_page,$offset);
    $result['page']=$page;
    $result['total']%$per_page==0?$result['total']=(int)($result['total']/$per_page):$result['total']=(int)($result['total']/$per_page+1);
    print_r(json_encode($result));
  }
  public function getallshopselect(){
    $result = $this->M_shop->getallshopselect();
    print_r(json_encode($result));
  }
  public  function brandtecount(){
    $shopid = $_GET['shopid'];
    header("Content-type:text/csv");
    header("Content-Disposition:attachment;filename='店铺数据资料.csv'");
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0'); header('Pragma:public');
    $str = "日期,品牌,终端业绩\n";
    echo $str;
    $countm=2;
    $countk=2;
    $adddaym=0;
    $adddayy=0;
    $count = 1000;
    $offset = 0;
    $result = $this->M_shop->getbrandtecount($shopid,$count,$offset);
    while($result['row']>0) {
      $result = $this->M_shop->getbrandtecount($shopid,$count,$offset);
      foreach ($result['result'] as $shop) {
        $month = explode("-", $shop['addday']);
        if($adddaym==0)$adddaym=$month[1];
        if($adddayy==0)$adddayy=$month[0];
        if($adddaym!=$month[1] || $adddayy!=$month[0]){
          echo $adddaym."(月)合计, ,=SUM(C".$countk.":C".--$countm.")\n";
          $countm+=1;
          $countk=$countm;
          $adddaym=$month[1];
          $adddayy=$month[0];
        }
        $str= $shop['addday'].",".$shop['bname'].",".$shop['tecount']."\n";
        echo $str;
        $countm++;
      }
      $offset +=$count;
    }
    if($countm!=2){
      echo $adddaym."(月)合计, ,=SUM(C".$countk.":C".--$countm.")\n";
    }
    return false;
  }
}
